package top.fengyulou.xiaofeng.controller.admin;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import top.fengyulou.xiaofeng.common.Jr;
import top.fengyulou.xiaofeng.common.UserUtils;
import top.fengyulou.xiaofeng.dto.MenuDto;
import top.fengyulou.xiaofeng.service.MenuService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author guochao
 * @version 1.0
 * @date 2022/1/10 22:17
 */
@Controller
public class AdminMenuController {
    @Resource
    private MenuService menuService;
    @Value("${sys.operate.close}")
    private Integer sysOperateClose;

    //列表页面
    @RequestMapping("/admin/menu/list")
    public String list() {
        return "admin/menu/admin-menu-list";
    }

    //列表页面
    @RequestMapping("/admin/menu/list2")
    public String list2() {
        return "admin/menu/admin-menu-list2";
    }

    //列表页面
    @RequestMapping("/admin/menu/list3")
    public String list3() {
        return "admin/menu/admin-menu-list3";
    }

    @RequestMapping("/admin/menu/child/table/tr")
    public String tableTr(MenuDto query, Model model) {
        model.addAttribute("list", menuService.list(query));
        return "admin/menu/admin-menu-table-tr";
    }

    @RequestMapping("/admin/menu/child/left/li")
    public String leftLi(Model model) {
        model.addAttribute("menuList", menuService.leftMenuList());
        return "admin/menu/admin-menu-left-li";
    }

    //菜单列表
    @RequestMapping("/admin/menu/ajax/list")
    @ResponseBody
    public Jr ajaxList(@RequestBody MenuDto menuDto) {
        menuService.listPage(menuDto);
        return Jr.yesd(menuDto);
    }

    //保存页面
    @RequestMapping("/admin/menu/save")
    public String save(String id, Model model) {
        if (id != null) {
            MenuDto data = menuService.getById(id);
            model.addAttribute("data", data);
        }
        return "admin/menu/admin-menu-save";
    }

    //保存
    @RequestMapping("/admin/menu/ajax/save")
    @ResponseBody
    public Jr ajaxSave(MenuDto menu, HttpServletRequest request) {
        if (sysOperateClose==0) {
            menu.setCurrentUserId(UserUtils.getUserId(request));
            menuService.save(menu);
        }
        return Jr.yes();
    }

    //删除
    @RequestMapping("/admin/menu/ajax/delete")
    @ResponseBody
    public Jr ajaxDelete(@RequestParam String[] ids) {
        if (sysOperateClose==0) {
            menuService.delete(ids);
        }
        return Jr.yes();
    }

    //修改显示状态
    @RequestMapping("/admin/menu/ajax/updateStatus")
    @ResponseBody
    public Jr ajaxUpdateStatus(@RequestParam String id, @RequestParam Integer status,HttpServletRequest request) {
        menuService.updateStatus(id, status,UserUtils.getUserId(request));
        return Jr.yes();
    }

    //修改排序
    @RequestMapping("/admin/menu/ajax/updateSort")
    @ResponseBody
    public Jr ajaxUpdateSort(@RequestParam String id, @RequestParam Integer sort,HttpServletRequest request) {
        menuService.updateSort(id, sort,UserUtils.getUserId(request));
        return Jr.yes();
    }
}
